﻿using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Portal.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace Com.FastSearch.SharePoint.WebControls.ESPSearchWebParts.UI
{
    /// <summary>
    /// Extended Search Box which in actual fact is a version of the default Search Center Search Box 
    /// with all the properties not directly supported by ESP disabled.
    /// For example the Scope in the Search Center is not mappeable to ESP as such.
    /// (Views or scope search can be used to to that)
    /// </summary>
    public class ESPSearchBox : SearchBoxEx
    {
        #region Private variables
   
        /// <summary>
        /// Default Constructor which disbled the Scope Drop Downs from the search box
        /// </summary>
        public ESPSearchBox()
        {
            DropDownModeEx = DropDownModesEx.HideScopeDD;
            DropDownMode = DropDownModes.HideDD_NoScope;
        }

        #endregion

        #region Disabled properties
        /// <summary>
        /// Disable the DropDownModesEx property to not be able to configure it in the web part editor 
        /// </summary>
        [Browsable(false), Resources("SearchBox_DDMode", "SearchBox_ScopeDD", "SearchBox_DDMode_ToolTip"), WebPartStorage(Storage.Shared)]
        public new DropDownModesEx DropDownModeEx
        {
            get
            {
                return base.DropDownModeEx;
            }
            set
            {
                base.DropDownModeEx = value;
            }
        }

        /// <summary>
        /// Disable the DropDownModes property to not be able to configure it in the web part editor 
        /// </summary>
        [Browsable(false), Resources("SearchBox_DDMode", "SearchBox_ScopeDD", "SearchBox_DDMode_ToolTip"), WebPartStorage(Storage.None)]
        public new DropDownModes DropDownMode {
            get {
                return base.DropDownMode; // DropDownModes.HideDD_NoScope;
            }
            set
            {
                base.DropDownMode = value;
            }
        }

        /// <summary>
        /// Disable the DropDownWidth property to not be able to configure it in the web part editor 
        /// </summary>
        [Browsable(false),Resources("SearchBox_DDWidth", "SearchBox_ScopeDD", "SearchBox_DDWidth_ToolTip"), DefaultValue(0), WebPartStorage(Storage.Shared)]
        public new int DropDownWidth
        {
            get
            {
                return base.DropDownWidth;
            }
            set
            {
                base.DropDownWidth = value;
            }
        }

        /// <summary>
        /// Disable the TextBeforeDropDown property to not be able to configure it in the web part editor 
        /// </summary>
        [Browsable(false), Resources("SearchBox_DDLabel", "SearchBox_ScopeDD", "SearchBox_DDLabel_ToolTip"), DefaultValue(""), WebPartStorage(Storage.Shared)]
        public new string TextBeforeDropDown
        {
            get
            { return base.TextBeforeDropDown;}
            set
            {
                base.TextBeforeDropDown = value;
            }
        }

        /// <summary>
        /// Disable the ScopeDisplayGroupName property to not be able to configure it in the web part editor 
        /// </summary>
        [Browsable(false), Resources("SearchBox_ScopeDisplayGroup", "", "SearchBox_ScopeDisplayGroup_ToolTip"), WebPartStorage(Storage.Shared)]
        public new string ScopeDisplayGroupName
        {
            get
            {
                return base.ScopeDisplayGroupName;
            }
            set
            {
                base.ScopeDisplayGroupName = value;
            }
        }

        /// <summary>
        /// Disable the AdvancedSearchPageURL property to not be able to configure it in the web part editor 
        /// </summary>
        [Browsable(false), WebPartStorage(Storage.Shared), Resources("SearchBox_AdvancedSearchURL", "", "SearchBox_AdvancedSearchURL_ToolTip")]
        public new string AdvancedSearchPageURL
        {
            get
            {
                return base.AdvancedSearchPageURL;
            }
            set
            {
                base.AdvancedSearchPageURL = value;
            }
        }

        /// <summary>
        /// Disable the ShowAdvancedSearch property to not be able to configure it in the web part editor 
        /// </summary>
        [Browsable(false), Resources("SearchBox_DisplayAdvSearchLink", "", "SearchBox_DisplayAdvSearchLink_ToolTip"), WebPartStorage(Storage.Shared), DefaultValue(false)]
        public new bool ShowAdvancedSearch
        {
            get
            {
                return base.ShowAdvancedSearch;
            }
            set
            {
                base.ShowAdvancedSearch = value;
            }
        }

        #endregion

        /// <summary>
        /// Set the WebPart to initially no use a frame aroud it.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            ChromeType = PartChromeType.None;
        }


        /// <inheritdoc/>
        [AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        protected override void OnPreRender(object sender, EventArgs e)
        {
            if (SearchResultPageURL != null && SearchResultPageURL.EndsWith("OSSSearchResults.aspx"))
            {
                SearchResultPageURL = Page.Request.Url.LocalPath;
            }            
            base.OnPreRender(sender, e);
        }
    }
}
